﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using KamRemakeRemake.Graphics;
using KamRemakeRemake.GameObjects.UnitObjects.Units;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace KamRemakeRemake.GameObjects.UnitObjects.Actions
{
    public abstract class UnitAction
    {
        protected string SpriteSuffix;

        protected Texture2D texture;

        protected AnimatedSprite currentSprite;

        public Unit Unit { get; set; }

        protected virtual bool CanExecute
        { get { return true; } }


        /// <summary>
        /// Execute an action for the specific unit
        /// </summary>
        /// <param name="spriteSuffix"></param>
        public virtual void Update(GameTime gameTime)
        {
            if (CanExecute)
            {
                this.currentSprite.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            this.currentSprite.Draw(spriteBatch, Unit.CurrentPosition);
        }

        public void LoadContent(ContentManager contentManager)
        {
            this.texture = contentManager.Load<Texture2D>(SpriteSuffix.GetSpriteName(Unit));
            this.currentSprite = new AnimatedSprite(this.texture, 2, 3);
        }
    }

    public static class UnitActionExtensions
    {
        public static string GetSpriteName(this string suffix, Unit unit)
        {
            if (unit == null)
            {
                return string.Empty;
            }

            var returnString = string.Format("{0}_{1}", unit.SpriteBaseName, unit.CurrentDirection);

            if (unit.CurrentAction == null)
            {
                returnString = string.Format("{0}_{1}", returnString, suffix);
            }

            return returnString;
        }
    }
}
